package GUI;

import IA.SystemClock;
import Simulation.CustomerSimulator;
import Simulation.KitchenSimulator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import system_interface.SystemInterface;

/**
 *
 * @author Rob Leister rleist1@students.towson.edu
 */
public class ManagersGUI extends javax.swing.JFrame {

    public void update() {
        jLabel1.setText("Sim Nums" + si.getNumberOfTables().toString() + ", "
                + Integer.toString(hostess.getEatTime()) + ", "
                + Integer.toString(hostess.getArrivalDelay()) + ", "
                + Integer.toString(kitchen.getGrillCapacity()) + ", "
                + Integer.toString(SystemClock.getInstance().getTimeScale()) + ", "
                + Double.toString(kitchen.getTimeReductionFactor())
        );
        jLabel9.setText(si.getNumberOfCustomersWaiting()); // tables
        jLabel10.setText(si.getNumberOfTables().toString());
        jLabel12.setText(si.getAverageCustomerWaitingTime().toString());
        jLabel14.setText(si.getAverageCookDeliveryTime().toString());
        String[] leastMost = si.GetMostLeastOrdered().split("/");
        jLabel23.setText(leastMost[0]);
        jLabel24.setText(leastMost[1]);
        jLabel29.setText(si.getAveragePrepTime().toString());
        displayReport(model);
        this.repaint();
    }//end update
    SystemInterface si;
    KitchenSimulator kitchen;
    CustomerSimulator hostess;
    String reportChoiceBoxLabels[] = {
        "Pick a Report",
        "Browse Menu",
        "Browse Menu by Price",
        "Browse Menu by Rating",
        "View Table Status",
        "View Customer Waiting Queue",
        "View Waiting To Cook Queue",
        "View Currently Cooking Queue"
    }; // end JboxLabels
    DefaultTableModel model;
    boolean newSelection = true;
    Double price = 0.0;
    Double rating = 0.0;

    public ManagersGUI() {
        initComponents();
    }

    public ManagersGUI(SystemInterface s, CustomerSimulator m, KitchenSimulator k) {
        si = s;
        hostess = m;
        kitchen = k;
        initComponents();
        model = (DefaultTableModel) ReportTable.getModel();
        model.setRowCount(0);
        ActionListener refresh = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                update();
            }
        };
        new javax.swing.Timer(1000, refresh).start();
    }

    public void displayReport(DefaultTableModel model) {
        model.setRowCount(0);
        if ((reportChoice.getSelectedItem()) == "View Waiting To Cook Queue") {
            displayOneColumn(model, si.getKitchenQueue().split("/"));
        }// end if ViewCustomerQueue
        if ((reportChoice.getSelectedItem()) == "View Currently Cooking Queue") {
            displayOneColumn(model, si.getItemsOnGrill().split("/"));
        }// end if ViewCustomerQueue
        if ((reportChoice.getSelectedItem()) == "View Customer Waiting Queue") {
            displayOneColumn(model, si.getCustomerQueue().split("/"));
        }// end if ViewCustomerQueue
        if ((reportChoice.getSelectedItem()) == "Browse Menu") {
            displayBrowseMenu(model, "Item, Current Price, Rating", si.browseMenu().split("/"));
        } //end if browse Menu
        if ((reportChoice.getSelectedItem()) == "Browse Menu by Price") {
            if (newSelection) {
                newSelection = false;
                price = getValidDouble("Browse by Price", "Enter price");
            }
            if (price >= 0) {
                displayBrowseMenu(model,
                        String.format("Item, Current Price, Rating (Under $ %.2f)", price),
                        si.browseMenuByPrice(price.toString()).split("/"));
            }// end if valid price
        } //end if browse Menu By Price
        if ((reportChoice.getSelectedItem()) == "Browse Menu by Rating") {
            if (newSelection) {
                newSelection = false;
                rating = getValidDouble("Browse by Rating", "Enter Rating");
                newSelection = false;
            }
            if (rating >= 0) {
                displayBrowseMenu(model, String.format("Item, Current Price, Rating (%.1f)", rating),
                        si.browseMenuByRating(rating.toString()).split("/"));
            } // end if valid rating
        } //end if browse Menu By Rating
        if ((reportChoice.getSelectedItem()) == "View Table Status") {

            String[] data = new String[1];
            String avail;
            for (int i = 0; i < Integer.parseInt(si.getNumberOfTables()); i++) {
                //  String iStr = Integer.toString(i);
                if (si.getTableStatus(i).equals("true")) {
                    avail = "Available";
                } else {
                    avail = ("Has " + si.getNumberOfCustomersAtTable(i) + " seated with " + si.getNumberOfOrdersAtTable(i) + " orders.");
                }
                data[0] = ((i + 1) + "  " + avail);
                model.addRow(data);
            }
        }// end if view Table Status
    } // end displayReport  

    Double getValidDouble(String title, String query) {
        Double d = -1.0;
        String s = (String) JOptionPane.showInputDialog(
                reportChoice, query, title, JOptionPane.QUESTION_MESSAGE);
        try {
            d = Double.parseDouble(s);
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        return d;
    } // end getValidDouble

    void displayOneColumn(DefaultTableModel model, String[] list) {
        String[] data = new String[1];
        if (list.length > 1) {
            for (int i = 0; i < list.length; i++) {
                data[0] = list[i];
                model.addRow(data);
            } // end for
        } // end if content
    }

    void displayTwoColumn(DefaultTableModel model, String[] list) {
        String[] data = new String[1];
        if (list.length > 1) {
            for (int i = 0; i < list.length - 1; i = i + 2) {
                data[0] = list[i] + " " + list[i + 1];
                model.addRow(data);
            } // end for
        } // end if content
    }

    void displayBrowseMenu(DefaultTableModel model, String header, String[] list) {
        String[] data = new String[1];
        if (list.length >= 4) {
            data[0] = header;
            model.addRow(data);
            data[0] = "";
            model.addRow(data);
            for (int i = 0; i < list.length; i = i + 4) {
                data[0] = list[i] + " -- " + list[i + 1] + "  $" + list[i + 2] + ", " + list[i + 3];
                model.addRow(data);
            } // end for
        } // end if content
        else {
            data[0] = "No items to display";
            model.addRow(data);
        }// end if else content

    } // end displayBrowseMenu

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        ReportTable = new javax.swing.JTable();
        reportChoice = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Managers View");
        setBounds(new java.awt.Rectangle(300, 0, 600, 600));

        ReportTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                ""
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ReportTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        ReportTable.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(ReportTable);
        ReportTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ReportTable.getColumnModel().getColumn(0).setResizable(false);

        reportChoice.setModel(new javax.swing.DefaultComboBoxModel(reportChoiceBoxLabels ));
        reportChoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportChoiceActionPerformed(evt);
            }
        });

        jLabel1.setText("Sim Numbers");

        jLabel6.setText("Tables");

        jLabel8.setText("Total Number of Tables");

        jLabel9.setText("numwaiting");

        jLabel10.setText("tottables");

        jLabel11.setText("Wait Time (mins)");

        jLabel12.setText("waittime");

        jLabel13.setText("Avg Cook & Deliv Time (mins)");

        jLabel14.setText("avgdelivtime");

        jLabel19.setText("Orders");

        jLabel20.setText("Most Order Item");

        jLabel21.setText("Least Order item");

        jLabel23.setText("mostordered");

        jLabel24.setText("leastordered");

        jLabel26.setText("Number of Parties Waiting");

        jLabel27.setText("Kitchen");

        jLabel28.setText("Avg Prep Time (mins)");

        jLabel29.setText("avgpreptime");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(reportChoice, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel26))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel12))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel14))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel20)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel24))
                    .addComponent(jLabel27, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel28)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel29))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(reportChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel26))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14))
                        .addGap(82, 82, 82)
                        .addComponent(jLabel19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20)
                            .addComponent(jLabel23))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel21)
                            .addComponent(jLabel24))
                        .addGap(38, 38, 38)
                        .addComponent(jLabel27)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel28)
                            .addComponent(jLabel29))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void reportChoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportChoiceActionPerformed
        newSelection = true;
        displayReport(model);

    }//GEN-LAST:event_reportChoiceActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;




                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ManagersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ManagersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ManagersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ManagersGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ManagersGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable ReportTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox reportChoice;
    // End of variables declaration//GEN-END:variables
}
